/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Groups;
import br.com.foxline.clinicplus.entity.SystemUser;
import br.com.foxline.clinicplus.service.GroupService;
import br.com.foxline.clinicplus.service.SystemUserService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.*;
import org.primefaces.model.DualListModel;

/**
 *
 * @author ely
 */
@ManagedBean
@ViewScoped
public class ManagerCreateUser implements Serializable {

    @EJB
    SystemUserService userService;
    @EJB
    GroupService groupsService;
    
    private SystemUser user;
    private Groups groups;
   
   
    private DualListModel<Groups> dualGrupo;
    private List<Groups> gruposSelec;
    
    private String confirm;

    @PostConstruct
    public void init() {
        user = new SystemUser();
        gruposSelec = new ArrayList<Groups>();
        dualGrupo = new DualListModel<Groups>(groupsService.findAll(), gruposSelec);
    }

    public void save() {
        user.setGroups(dualGrupo.getTarget());
        userService.saveUser(confirm, user);
        init();
    }
    
    
     public List<SystemUser> autocomplete(String query){
        return userService.findAll();
    }
    
    public List<Groups> groups(){
        return groupsService.findAll();
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public SystemUser getUser() {
        return user;
    }

    public void setUser(SystemUser user) {
        this.user = user;
    }

    public Groups getGroups() {
        return groups;
    }

    public void setGroups(Groups groups) {
        this.groups = groups;
    }

 
    
    public DualListModel<Groups> getDualGrupo() {
         dualGrupo = new DualListModel<Groups>(groupsService.findAll(), gruposSelec);
        return dualGrupo;
    }

    public void setDualGrupo(DualListModel<Groups> dualGrupo) {
        this.dualGrupo = dualGrupo;
    }
    
    
}
